<?php
namespace app\dsadmin\controller;
use app\dsadmin\model\ConfigModel;
use think\Db;
use think\Cache;

class Config extends Base
{
    /**
     * [index 配置列表]
     * 
     * @author [dashang] 
     */
    public function index()
    {
        $key = input('key');
        $map = [];
        if ($key && $key !== "")
        {
            $map['title'] = ['like', "%" . $key . "%"];
        }
        $config = new ConfigModel();
        $nowpage = input('get.page') ? input('get.page'):1;
        $limits = 10; // 获取总条数
        $count = $config->getAllCount($map); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $list = $config->getAllConfig($map, $nowpage, $limits);
        $this->assign('nowpage', $nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * [add_config 添加配置]
     * 
     * @author [dashang] 
     */
    public function add_config()
    {
        if (request()->isAjax())
        {
            $param = input('post.');
            $config = new ConfigModel();
            $flag = $config->insertConfig($param);
            cache('db_config_data', null);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch();
    }

    /**
     * [edit_config 编辑配置]
     * 
     * @author [dashang] 
     */
    public function edit_config()
    {
        $config = new ConfigModel();
        if (request()->isAjax())
        {
            $param = input('post.');
            $flag = $config->editConfig($param);
             cache('db_config_data', null);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $info = $config->getOneConfig($id);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * [del_config 删除配置]
     * 
     * @author [dashang] 
     */
    public function del_config()
    {
        $id = input('param.id');
        $config = new ConfigModel();
        $flag = $config->delConfig($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [user_state 配置状态]
     * 
     * @author [dashang] 
     */
    public function status_config()
    {
        $id = input('param.id');
        $status = Db::name('config')->where(array('id' => $id))->value('status'); //判断当前状态情况
        if ($status == 1)
        {
            $flag = Db::name('config')->where(array('id' => $id))->setField(['status' => 0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('config')->where(array('id' => $id))->setField(['status' => 1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    }

    /**
     * [获取某个标签的配置参数]
     * 
     * @author [dashang] 
     */
    public function group()
    {
       
        $id = input('id', 1);
        $type = config('config_group_list');
         
        
        $list = Db::name("Config")->where(array('status' => 1, 'group' => $id))->field('id,name,title,extra,value,remark,type')->order('sort')->select();
        if ($list)
        {
            $this->assign('list', $list);
        }
        
        
        $this->assign('id', $id);
        return $this->fetch();
    }

    /**
     * [批量保存配置]
     * 
     * @author [dashang] 
     */
    public function save($config)
    {
        if ($config && is_array($config))
        {
            $Config = Db::name('Config');
            foreach ($config as $name => $value)
            {
                if($name == "yizhifu"){
                    Cache::store('redis')->set('ylyizhifu',$value,5253120);
                }
                if($name == "caihong"){
                     Cache::store('redis')->set('ylcaihong',$value,5253120);
                }
                if($name == "huangqius"){
                     Cache::store('redis')->set('ylhuangqius',$value,5253120);
                }
                
                 if($name == "xinpay"){
                     Cache::store('redis')->set('ylxinpay',$value,5253120);
                }
              
              
                
                if($name == "bluePay"){
                     Cache::store('redis')->set('ylbluePay',$value,5253120);
                }
                
                if($name == "cdnz_url"){
                    Cache::store('redis')->set('ylcdnz_url',$value,5253120);
                }
                if($name == "cdnb_url"){
                    Cache::store('redis')->set('ylcdnb_url',$value,5253120);
                }
                if($name == "cdnc_url"){
                    Cache::store('redis')->set('ylcdnc_url',$value,5253120);
                }
                
                if($name == "bananapaynew"){
                     Cache::store('redis')->set('ylbananapaynew',$value,5253120);
                }
                
                if($name == "cdne_url"){
                    Cache::store('redis')->set('ylcdne_url',$value,5253120);
                }
                
                if($name == "cdnf_url"){
                    Cache::store('redis')->set('ylcdnf_url',$value,5253120);
                }
                
                if($name == "cdng_url"){
                    Cache::store('redis')->set('ylcdng_url',$value,5253120);
                }
                
                
                if($name == "image_urlz"){
                    Cache::store('redis')->set('ylimage_urlz',$value,5253120);
                    Cache::store('redis')->set('tsimage_urlz',$value,5253120);
                }
                if($name == "image_urlb"){
                    Cache::store('redis')->set('ylimage_urlb',$value,5253120);
                    Cache::store('redis')->set('tsimage_urlb',$value,5253120);
                }
                if($name == "image_urlc"){
                    Cache::store('redis')->set('ylimage_urlc',$value,5253120);
                    Cache::store('redis')->set('tsimage_urlc',$value,5253120);
                }
                
                if($name == "image_urle"){
                    Cache::store('redis')->set('ylimage_urle',$value,5253120);
                    Cache::store('redis')->set('tsimage_urle',$value,5253120);
                }
                
                if($name == "image_urlf"){
                    Cache::store('redis')->set('ylimage_urlf',$value,5253120);
                    Cache::store('redis')->set('tsimage_urlf',$value,5253120);
                }
                
                
                 if($name == "image_urlg"){
                    Cache::store('redis')->set('ylimage_urlg',$value,5253120);
                    Cache::store('redis')->set('tsimage_urlg',$value,5253120);
                }
                
                
              
                if($name == "neifu"){
                    Cache::store('redis')->set('ylneifu',$value,5253120);
                }
                
                if($name == "yy"){
                    Cache::store('redis')->set('ylyy',$value,5253120);
                }
                
                if($name == "qlpay"){
                    Cache::store('redis')->set('ylqlpay',$value,5253120);
                }
                
                if($name == "yizhifu"){
                    Cache::store('redis')->set('ylyizhifu',$value);
                }
                
                if($name == "imgbase"){
                    Cache::store('redis')->set('ylimgbase',$value);
                }
                
                 if($name == "gema"){
                    Cache::store('redis')->set('ylgema',$value,5253120);
                }
               
                if($name == "indexsj"){
                    Cache::store('redis')->set('ylindexsj',$value,5253120);
                }
                
                  if($name == "caihongpay"){
                    Cache::store('redis')->set('ylcaihongpay',$value,5253120);
                }
                
                  if($name == "saomahuang"){
                    Cache::store('redis')->set('ylsaomahuang',$value,5253120);
                }
                
                 if($name == "taoqimao"){
                    Cache::store('redis')->set('yltaoqimao',$value,5253120);
                }
                
                   if($name == "meimiaopay"){
                    Cache::store('redis')->set('ylmeimiaopay',$value,5253120);
                }
                
                  if($name == "huaguanpay"){
                    Cache::store('redis')->set('ylhuaguanpay',$value,5253120);
                }
                
                  if($name == "jiuxunpay"){
                    Cache::store('redis')->set('yljiuxunpay',$value,5253120);
                }
                
                  if($name == "caihongpay"){
                    Cache::store('redis')->set('ylcaihongpay',$value,5253120);
                }
                
                  if($name == "caihongpaygema"){
                    Cache::store('redis')->set('ylcaihongpaygema',$value,5253120);
                }
                
                 if($name == "gushu"){
                    Cache::store('redis')->set('ylgushu',$value,5253120);
                }
                
                if($name == "laoyang"){
                    Cache::store('redis')->set('yllaoyang',$value,5253120);
                }
                
                
                if($name == "goidpay"){
                    Cache::store('redis')->set('ylgoidpay',$value,5253120);
                }
                
                if($name == "labipay"){
                    Cache::store('redis')->set('yllabipay',$value,5253120);
                }
                
                if($name == "sfpay"){
                    
                    Cache::store('redis')->set('sifangpay',$value,5253120);
                }
                
                if($name == "kouliangs"){
                //   if($value == 0){
                //       Db::name('kou')->where(['ns' => 10])->update(['ns' => 7,'cs' => 2]);
                //     }else{
                //       Db::name('kou')->where(['ns' => 7])->update(['ns' => 10,'cs' => 2]);
                //     }
                //     Db::name('config')->where(['id'=> 1223])->update(['value'=>$value]);
                }else{
                    $map = array('name' => $name);
                    $Config->where($map)->setField('value', $value,5253120);
                }
            }
        }
        cache('db_config_data', null);
        // $url = 'http://web.beiyuehealth.cn/index/index/test';
        // getcurl($url,'abc');
        $this->success('保存成功！');
    }
}
